<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/14
 * Time: 17:14
 */

namespace Admin\Model;

use Think\Model;

class DepartModel extends Model{

    /**递归显示所有学院，院系，班级
     * @param int $f_id
     * @param array $result
     * @param int $spac
     * @return array
     */
    public function getAllDepartWhere($f_id = 0, &$result=array(), $spac = 0){
        $spac = $spac + 2;
        $res = $this->where("f_id = '$f_id'")->select();
        for($i = 0; $i < count($res); $i++){
            $res[$i]['name'] = str_repeat('&nbsp;',$spac).'|——'.$res[$i]['name'];
            $result[] = $res[$i];
            $this->getAllDepartWhere($res[$i]['id'],$result,$spac);
        }
        return $result;
    }

    /**
     * 删除
     * $data['f_id'] == 0 删除
     * @param $data
     * @return string
     */
    public function delDepart($data){
        $id = $data['id'];
        $this->where("f_id = $id")->delete();
        $flag = $this->delete($id);
        if ($flag !== false ) return '000000';
        return '111111';
    }

    /**是否存在此id
     * @param $id
     * @return string
     */
    public function getDelDepart($id){
        $flag = $this->delete($id);
        if ($flag !== false )return '000000';
        return '111111';
    }

    /**添加
     * @param $data
     * @return string
     */
    public function addDepart($data){
        $flag = $this->add($data);
        if ($flag !== false) return '000000';
        return '111111';
    }

    /**修改
     * @param $data
     * @return string
     */
    public function editDepart($data){
        $flag = $this->save($data);
        if ($flag !== false )return '000000';
        return '111111';
    }

    /**获取单条记录
     * @param $data
     * @return mixed
     */
    public function getDepartById($data){
        $id = $data['id'];
        $result = $this->find($id);
        return $result;
    }

    /**
     * 根据班级的f_id，返回专业
     * @param $f_id
     * @return mixed
     */
    public function getZyById($f_id){
        $id = $f_id;
        $result = $this->field('id,f_id')->find($id);
        return $result;
    }
    /**
     * 所有二级学院信息
     * @return 所有二级学院id,name
     */
    public function getAllXyname(){
        return  $this->field('id,name')->where("f_id = 0")->select();
    }

    /**
     * 专业
     * @param $id：学院id
     * @return mixed:该id下所有专业
     */
    public function getAllXyById($id){
        return  $this->field('id,name')->where("f_id = $id")->select();
    }

    /**班级
     * @param $id：专业id
     * @return mixed：该专业id下所有的班级
     */
    public function getAllBjById($id){
        return  $this->field('id,name')->where("f_id = $id")->select();
    }


    /**验证专业是否属于学院
     * @param $data
     * @return string
     */
    public function getYz($data,$x_id){

        $z_id = $data['f_id'];
        $rs = $this->where("id = $z_id")->find();
        // echo $this->getLastsql();die;
        if ($rs['f_id']  != $x_id)
            return '000000';
        return '111111';

    }

}